using System;
using System.Data;
using System.Configuration;
using System.Web.UI.WebControls;
using System.IO;
using System.Xml;

public partial class Controls_ViewMedication : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            HiddenFieldSortExpression.Value = "LastUpdated";
            HiddenFieldSortOrder.Value = "ASC";
            BindGridView();
        }
    }
    private void BindGridView()
    {
        if (!string.IsNullOrEmpty(ControlHelperClass.MedicationFolderPath))
        {
            DirectoryInfo dirInfo = new DirectoryInfo(ControlHelperClass.MedicationFolderPath);
            FileInfo[] fileInfo = dirInfo.GetFiles("*.xml");
            DataTable LabResultTable = new DataTable();
            LabResultTable.Columns.Add("Medication_Document");
            LabResultTable.Columns.Add("LastUpdated");
            foreach (FileInfo finfo in fileInfo)
            {
                if (finfo.Name.Contains(ControlHelperClass.PatientId.ToString()))
                {
                    XmlDocument xmldoc = new XmlDocument();
                    xmldoc.Load(finfo.FullName);
                    if (!string.IsNullOrEmpty(xmldoc.SelectSingleNode("/Medications").InnerXml))
                    {
                        object[] parameter = new object[2];
                        parameter[0] = finfo.Name;
                        parameter[1] = finfo.LastWriteTime;
                        LabResultTable.Rows.Add(parameter);
                    }
                }
            }
            if (LabResultTable.Rows.Count > 0)
            {
                LabResultTable.DefaultView.Sort = HiddenFieldSortExpression.Value + " " + HiddenFieldSortOrder.Value;
                GridViewMedications.DataSource = LabResultTable.DefaultView;
                GridViewMedications.DataBind();
                LabelNoRecords.Visible = false;
            }
            else
            {
                LabelNoRecords.Text = "No Medication results found.";
                LabelNoRecords.Visible = true;
            }
        }
    }
    public void Button1_Click(object o, EventArgs e)
    {
        LinkButton Button1 = (LinkButton)o;
        GridViewRow grdRow = (GridViewRow)Button1.Parent.Parent;
        LinkButton documentName = (LinkButton)grdRow.Cells[0].FindControl("Medicationsdocument");
        string filename = documentName.Text;
        string medicationfilePath = Path.Combine(ControlHelperClass.MedicationFolderPath, filename);
        if (!string.IsNullOrEmpty(medicationfilePath))
        {
            DivMedicationXml.Visible = true;
            using (StreamReader medicationStream = new StreamReader(medicationfilePath, true))
            {
                DivMedicationXml.InnerHtml = ControlHelperClass.HandleJunkChar(medicationStream.ReadToEnd());
            }
        }
    }

    protected void GridViewMedications_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridViewMedications.PageIndex = e.NewPageIndex;
        BindGridView();
    }
    protected void GridViewMedications_Sorting(object sender, GridViewSortEventArgs e)
    {
        HiddenFieldSortExpression.Value = e.SortExpression;
        if (!HiddenFieldSortOrder.Value.Equals("ASC"))
        {
            HiddenFieldSortOrder.Value = "ASC";
        }
        else
        {
            HiddenFieldSortOrder.Value = "DESC";
        }
        BindGridView();
    }
}
